package com.simen.hellobytes.code;


import com.simen.hellobytes.IOUtil;
import com.simen.hellobytes.interfaces.LineIterator;

import java.io.File;
import java.io.IOException;

/**
 * author: SimenHi
 * create time: 2020-08-10 09:59
 * description:
 */
public class FilterCommentLine {

    public static void main(String[] args) {
        File file = new File("RSS14Reader.java");

        final FilterCommentLine commentLine = new FilterCommentLine();
        final StringBuilder builder = new StringBuilder();
        try {
            IOUtil.iteratorFileLine(file, new LineIterator() {
                public void onRead(String line) throws IOException {
                    String noCommentLine = commentLine.swallowLine(line.trim());

                    if (noCommentLine != null) {
                        builder.append(noCommentLine);
                        builder.append("\n");
                    }
                }
            });

            IOUtil.writeContent(builder.toString().getBytes(), "1111.java");

            commentLine.swallowEnd();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static final int LANGUAGE_JAVASCRIPT=1;
    public static final int LANGUAGE_JAVA=0;
    public static final int LANGUAGE_HTML=2;
    public static final int LANGUAGE_C=3;

    private boolean foundSuperComment = false;
    private String charset="UTF-8";
    private int languageType=LANGUAGE_JAVA;

    public FilterCommentLine() {
    }

    public FilterCommentLine(int languageType) {
        this.languageType = languageType;
    }

    public int getLanguageType() {
        return languageType;
    }

    /**
     * @return 返回过滤掉注释内容的行
     */
    public String swallowLine(String trimLine) {
        int charL = trimLine.length();
        int numQuotes = 0;
        char[] trimLineChars = new char[charL];
        int trimLineCharsLength = 0;
        for (int i = 0; i < charL; i++) {
            char c = trimLine.charAt(i);

            if (foundSuperComment) {
                if (c == '*') {
                    int nextCharType = GrammarUtil.charType(trimLine, i + 1);
                    if (nextCharType == GrammarUtil.CHAR_TYPE_SLASH) {
                        foundSuperComment = false;
                        i++;
                    }
                }
            } else {
                if (i == 0) {
                    if (c == '/') {
                        int nextCharType = GrammarUtil.charType(trimLine, i + 1);
                        if (nextCharType == GrammarUtil.CHAR_TYPE_SLASH) {
                            //如果一行字符串只有注释没有代码时,直接忽略
                            break;
                        } else if (nextCharType == GrammarUtil.CHAR_TYPE_STAR) {
                            foundSuperComment = true;
                            i++;
                            continue;
                        }
                    } else if (c == '"') {
                        if (GrammarUtil.beforeQuoteContainsDoubleBackSlash(trimLine, i - 1)) {
                            numQuotes++;
                        }
                    }

                    trimLineCharsLength++;
                    trimLineChars[trimLineCharsLength - 1] = c;
                } else {
                    if(numQuotes % 2 == 0) {
                        if (c == '/') {
                            int nextCharType = GrammarUtil.charType(trimLine, i + 1);
                            if (nextCharType == GrammarUtil.CHAR_TYPE_SLASH) {
                                //如果一行字符串只有注释没有代码时,直接忽略
                                break;
                            } else if (nextCharType == GrammarUtil.CHAR_TYPE_STAR) {
                                foundSuperComment = true;
                                i++;
                                continue;
                            }
                        } else if (c == '"') {
                            if (GrammarUtil.beforeQuoteContainsDoubleBackSlash(trimLine, i - 1)) {
                                numQuotes++;
                            }
                        }
                    }

                    trimLineCharsLength++;
                    trimLineChars[trimLineCharsLength - 1] = c;
                }
            }
        }

        return trimLineCharsLength == 0 ? null : new String(trimLineChars, 0, trimLineCharsLength);
    }

    public void resetTrim(){
        foundSuperComment=false;
    }

    public boolean isSuperCommentClosed() {
        return !foundSuperComment;
    }

    public void swallowEnd() throws IOException {
        if(foundSuperComment){
            throw new IOException("包含不正确的注释内容");
        }
    }

}
